package com.common.utils;

import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author liuxuhuang
 * @date 2023年03月30日 22:03
 */
@Component
public class StringUtil {
    /**
     * 移除字符串中空格
     *
     * @param val
     * @return String
     */
    public String removeStringBlock(String val) {
        return val.replaceAll(" ", "");
    }

    /**
     * 产生随机字符串
     *
     * @param length
     * @return
     */
    public String getRandomString(int length) {
        String string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(62);
            sb.append(string.charAt(number));
        }
        return String.valueOf(sb);
    }

    /**
     * 随机产生用户名
     *
     * @param length
     * @return
     */
    public String getUserName(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String CharORNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if ("char".equalsIgnoreCase(CharORNum)) {
                int temp = random.nextInt(2) % 2 == 0 ? 64 : 97;
                val += String.valueOf(random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(CharORNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
